package com.alkaid.anime.controller;

import com.alkaid.anime.service.CoinsService;
import com.alkaid.commons.controller.BaseController;
import com.alkaid.commons.entity.Coins;
import com.alkaid.commons.utils.JsonResult;
import com.alkaid.commons.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author Kylin
 */
@RestController
@RequestMapping("api/anime")
public class CoinsController extends BaseController {

    private CoinsService coinsService;

    @Autowired
    public void setCoinsService(CoinsService coinsService) {
        this.coinsService = coinsService;
    }

    @GetMapping("/coins/{animeId}")
    public JsonResult<Boolean> getThumb(HttpServletRequest request, @PathVariable Integer animeId) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        Boolean result = coinsService.getCoins(userId, animeId);
        return new JsonResult<>(200, result, "success");
    }

    @PostMapping("/coins")
    public JsonResult<Void> setThumb(HttpServletRequest request, @Validated Coins coins) {
        String jwt = request.getHeader("Authorization");
        JwtUtils jwtUtils = new JwtUtils();
        jwtUtils.parseJwt(jwt);
        Long userId = jwtUtils.getUser().getId();
        coins.setUserId(userId);
        coinsService.setCoins(coins);
        return new JsonResult<>(200, null, "投币成功(✪ω✪)");
    }
}
